<template>
  <div class="layout-body">
    <el-container>
      <el-aside class="aside" :style="{ width: asideWidth + 'px' }">
        <div class="logo">
          <span v-if="!menuCollapse">CodeHub管理后台</span>
          <span v-else
            ><img src="../../public/CodeHub.png" :style="{ width: '40px' }"
          /></span>
        </div>
        
        

        <div class="menu-panel">
          <el-menu
            :default-openeds="defaultOpeneds"
            :collapse-transition="false"
            :collapse="menuCollapse"
            class="el-menu-vertical-demo"
            background-color="#3d3c4a"
            text-color="#fff"
            router
            :default-active="defaultActive"
          >
            <template v-for="item in userInfo.menuList">
              <el-sub-menu
                :index="item.menuUrl"
                v-if="item.children"
                :key="item.menuUrl"
              >
                <template #title>
                  <i :class="['iconfont', item.icon]"></i>
                  <span class="menu-name">{{ item.menuName }}</span>
                </template>
                <el-menu-item
                  v-for="subItem in item.children"
                  :index="subItem.menuUrl"
                  :key="subItem.menuUrl"
                >
                  <span class="menu-name">{{ subItem.menuName }}</span>
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="item.menuUrl" :key="item">
                <template #title>
                  <i :class="['iconfont', item.icon]"></i>
                  <span class="menu-name">{{ item.menuName }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-menu>
        </div>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div
            :class="[
              'op-menu',
              'iconfont',
              menuCollapse ? 'icon-expand' : 'icon-collapse',
            ]"
            @click="opMenu"
          ></div>

          <div class="menu-bread">
            <el-breadcrumb>
              <template v-for="item in menuBreadCrumbList">
                <el-breadcrumb-item v-if="item.menuName" :key="item.menuName">
                  {{ item.menuName }}
                </el-breadcrumb-item>
              </template>
            </el-breadcrumb>
          </div>

          <div class="userinfo">
        欢迎回来，
        <el-dropdown>
          <span class="name">{{ userInfo.userName }}</span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="updateMyPwd">修改密码</el-dropdown-item>
              <el-dropdown-item @click="logout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
          
        </el-header>
        <el-main class="main-content">
          <div class="tag-content">
            <el-tabs
              type="border-card"
              v-model="defaultActive"
              @tab-change="tabClick"
              @edit="editTab"
            >
              <el-tab-pane
                v-for="item in tabList"
                :name="item.menuUrl"
                :label="item.menuName"
                :closable="tabList.length > 1"
                :key="item.menuUrl"
              ></el-tab-pane>
            </el-tabs>
          </div>
          <div class="content-body">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
    <UpdatePwd ref="updatePwdRef"></UpdatePwd>
  </div>
</template>

<script setup>
import { watch } from "vue";
import UpdatePwd from "./UpdatePwd.vue";
import { ref, reactive, getCurrentInstance } from "vue";
import { useRoute, useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();

const userInfo = ref(
  JSON.parse(sessionStorage.getItem("userInfo")) || { menuList: [] }
);
const menuList = userInfo.value.menuList;

//默认选中
const defaultActive = ref();

const menuMap = {};
//默认展开菜单
const defaultOpeneds = ref([]);
const init = () => {
  menuList.forEach((item) => {
    defaultOpeneds.value.push(item.menuUrl);
    item.children.forEach((subItem) => {
      menuMap[subItem.menuUrl] = subItem;
    });
  });
};

init();

//收起关闭菜单
const menuCollapse = ref(false);
const asideWidth = ref(200);
const opMenu = () => {
  menuCollapse.value = !menuCollapse.value;
  if (menuCollapse.value) {
    asideWidth.value = 63;
  } else {
    asideWidth.value = 200;
  }
};

//tab操作
const tabList = ref([]);
const tabClick = (e) => {
  router.push(e);
};
const editTab = (targetKey, action) => {
  if (action !== "remove") {
    return;
  }
  let currentPath = defaultActive.value;
  let tabs = tabList.value.slice();
  const currentIndex = tabs.findIndex((tab) => tab.menuUrl === targetKey);
  if (currentIndex !== -1) {
    if (currentIndex === tabs.length - 1 && currentIndex > 0) {
      currentPath = tabs[currentIndex - 1].menuUrl;
    } else if (currentIndex < tabs.length - 1) {
      currentPath = tabs[currentIndex + 1].menuUrl;
    }
  }
  //关闭当前选中的tab

  tabList.value = tabs.filter((tab) => {
    return tab.menuUrl !== targetKey;
  });
  if (currentPath !== defaultActive.value) {
    router.push(currentPath);
  }
};

//面包屑导航
const menuBreadCrumbList = ref([]);
watch(
  () => route,
  (newVal, oldVal) => {
    defaultActive.value = newVal.path;
    menuBreadCrumbList.value = newVal.matched;

    let currentMune = tabList.value.find((item) => {
      return item.menuUrl === defaultActive.value;
    });
    if (!currentMune && menuMap[newVal.path]) {
      tabList.value.push(menuMap[newVal.path]);
    }
  },
  { immediate: true, deep: true }
);

//退出
const logout = () => {
  proxy.Confirm("确定退出吗?", async () => {
    let result = await proxy.Request({
      url: "/logout",
    });
    if (!result) {
      return;
    }
    sessionStorage.removeItem("userInfo");
    router.push("/login");
  });
};
const updatePwdRef = ref();
const updateMyPwd = () => {
  updatePwdRef.value.show();
};
</script>

<style lang="scss">
//菜单收起样式
.el-popper {
  border: none !important;
  .el-menu-item.is-active {
    color: #fff;
    background: var(--el-color-primary);
  }
  .el-menu-item:hover {
    color: #d8d8ee;
  }
  .el-menu--popup {
    padding: 0px;
  }
}
.layout-body {
  .logout {
    margin-left: 15px;
    position: absolute;
    right: 2%;
    cursor: pointer;
  }
  .userinfo {
      margin-right: 20px;
      font-size: 14px;
      display: flex;
      position: absolute;
      right: 2%;
      align-items: center;
      .name {
        font-size: 1;
        color: #409eff;
        cursor: pointer;
      }
      .user-type {
        margin-left: 5px;
      }
      .logout {
        margin-left: 15px;
      }
    }
  .logout:hover {
    color: #409eff;
  }
  .aside {
    background: #3d3c4a;
    transition: 0.2s;
    .logo {
      display: flex;
      height: 50px;
      color: #fff;
      background: #7e7b9c;
      align-items: center;
      font-size: 18px;
      padding-left: 5px;
    }
    .menu-panel {
      height: calc(100vh - 50px);
      .menu-name {
        padding-left: 10px;
      }
      //去除边框
      .el-menu {
        border-right: none;
      }
      //每个菜单的颜色
      .el-menu-item {
        background: #353544;
      }
      //选中的颜色
      .el-menu-item.is-active {
        color: #fff;
        background: var(--el-color-primary);
      }
      //鼠标移上去的颜色
      .el-menu-item:hover {
        color: #d8d8ee;
      }
    }
  }
  .header {
    background: #fff;
    border-bottom: 1px solid #ddd;
    height: 50px;
    line-height: 50px;
    padding: 0px 10px !important;
    display: flex;
    align-items: center;
    .op-menu {
      font-size: 20px;
      cursor: pointer;
      color: #3a3a40;
    }
    .menu-bread {
      margin-left: 10px;
    }
  }

  .main-content {
    padding: 0px;
    .tag-content {
      .el-tabs--border-card {
        border: none;
      }
      .el-tabs__content {
        display: none;
      }
    }
    .content-body {
      overflow: hidden;
      padding: 10px 10px 5px 10px;
    }
  }
}
</style>
